package com.msh.automatic.android.api.controller.device;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.msh.automatic.android.api.entity.ActivationCode;
import com.msh.automatic.android.api.entity.Device;
import com.msh.automatic.android.api.entity.TaskLog;
import com.msh.automatic.android.api.mapper.ActivationCodeMapper;
import com.msh.automatic.android.api.mapper.DeviceMapper;
import com.msh.automatic.android.api.mapper.TaskLogMapper;
import com.msh.automatic.android.api.util.DeviceActivationInfoUtil;
import com.msh.automatic.android.api.util.EmqxUtil;
import com.msh.automatic.android.api.util.JwksUtil;
import com.msh.automatic.android.api.util.RetUtil;
import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/d/device")
public class DeviceController {

    @Resource
    JwksUtil jwksUtil;
    @Resource
    DeviceMapper deviceMapper;
    @Resource
    ActivationCodeMapper activationCodeMapper;
    @Resource
    TaskLogMapper taskLogMapper;

    @ApiOperation(value = "设备心跳")
    @RequestMapping(value = "/heartbeat", method = RequestMethod.POST)
    public Object heartbeat(@RequestBody Device params) throws UnsupportedEncodingException {
        Device device = deviceMapper.selectById(DeviceActivationInfoUtil.getDeviceId());
        device.setHeartbeat(new Date());
        device.setOnline(EmqxUtil.clientOnlineStatus(device.getId()));
        device.setAccessibility(params.getAccessibility());
        device.setPower(params.getPower());
        device.setFileManager(params.getFileManager());
        device.setCurrentTaskLogId(params.getCurrentTaskLogId());
        device.setInstallApk(params.getInstallApk());
        device.setControlAppVersion(params.getControlAppVersion());
        DeviceActivationInfoUtil.isActivation(device);
        deviceMapper.updateById(device);
        if(StrUtil.isNotEmpty(device.getCurrentTaskLogId())){
            TaskLog taskLog = taskLogMapper.selectById(device.getCurrentTaskLogId());
            return RetUtil.success(taskLog);
        }
        return RetUtil.success();
    }

    @ApiOperation(value = "设备调试端口更新")
    @RequestMapping(value = "/debugAddress", method = RequestMethod.POST)
    public Object debugAddress(@RequestBody Device params) throws UnsupportedEncodingException {
        Device device = deviceMapper.selectById(DeviceActivationInfoUtil.getDeviceId());
        if(!params.getVpnIp().equals(device.getVpnIp())){
            log.error("设备vpn地址与上报地址不匹配： vpn:{}  param:{} id:{}",device.getVpnIp(),params.getVpnIp(),device.getId());
        }
        device.setDebugPort(params.getDebugPort());
        deviceMapper.updateById(device);
        return RetUtil.success(device);
    }

}
